package com.example.weight.weidgets;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.weight.R;

public class CustomDialog extends Dialog {
    private Context context;
    private TextView titleView;
    private TextView msgView;
    private LinearLayout topMsgParentView;
    private LinearLayout mainBodyParentView;
    private TextView leftView;
    private TextView rightView;
    private View view;
    private CustomDialog customDialog;

    private CustomDialog(Context context) {
        super(context, R.style.AlertDialogStyle);
        this.context = context;
        view = getLayoutInflater().inflate(R.layout.dialog_custom, null);
        setContentView(view);
        initView();
        customDialog = this;
    }

    private void initView() {
        titleView = view.findViewById(R.id.tv_title);
        msgView = view.findViewById(R.id.tv_msg);
        topMsgParentView = view.findViewById(R.id.ll_top_msg);
        mainBodyParentView = view.findViewById(R.id.ll_main_body);
        leftView = view.findViewById(R.id.tv_left);
        rightView = view.findViewById(R.id.tv_right);
    }

    private void setTitle(String title) {
        this.titleView.setText(title);
    }

    private void setTitleColor(int color) {
        this.titleView.setTextColor(color);
    }

    private void setMsg(String msg) {
        this.msgView.setText(msg);
        this.msgView.setVisibility(View.VISIBLE);
    }

    private void addTopMsgView(View view) {
        this.topMsgParentView.removeAllViews();
        this.topMsgParentView.addView(view);
        this.topMsgParentView.setVisibility(View.VISIBLE);
    }

    private void addMainBodyView(View view) {
        this.mainBodyParentView.removeAllViews();
        this.mainBodyParentView.addView(view);
        this.mainBodyParentView.setVisibility(View.VISIBLE);
    }

    private void setLeftButton(String text, final OnClickListener listener) {
        this.leftView.setText(text);
        this.leftView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null)
                    listener.onClick(customDialog);
            }
        });
        this.leftView.setVisibility(View.VISIBLE);
    }

    private void setRightButton(String text, final OnClickListener listener) {
        this.rightView.setText(text);
        this.rightView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null)
                    listener.onClick(customDialog);
            }
        });
        this.rightView.setVisibility(View.VISIBLE);
    }

    public interface OnClickListener {
        void onClick(CustomDialog dialog);
    }

    @Override
    public void show() {
        super.show();
        /**
         * 设置宽度全屏，要设置在show的后面
         */
        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
        layoutParams.gravity=Gravity.CENTER;
        layoutParams.width= WindowManager.LayoutParams.MATCH_PARENT;
        layoutParams.height= WindowManager.LayoutParams.MATCH_PARENT;
        getWindow().getDecorView().setPadding(0, 0, 0, 0);
        getWindow().setAttributes(layoutParams);
    }

    @Override
    public void dismiss() {
        super.dismiss();
    }

    public View getMsgParentView(){
        return topMsgParentView;
    }

    public View getBodyParentView(){
        return mainBodyParentView;
    }

    public static class Builder {
        Context context;
        String title;
        int color;
        String msg;
        View topMsgView;
        View mainBodyView;
        String leftText;
        String rightText;
        OnClickListener leftListener;
        OnClickListener rightListener;

        public Builder(Context context) {
            this.context = context;
        }

        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        public Builder setTitleColor(int color) {
            this.color = color;
            return this;
        }

        public Builder setMsg(String msg) {
            this.msg = msg;
            return this;
        }

        public Builder addTopMsgView(View view) {
            this.topMsgView = view;
            return this;
        }

        public Builder addMainBodyView(View view) {
            this.mainBodyView = view;
            return this;
        }

        public Builder setLeftButton(String text, OnClickListener listener) {
            this.leftText = text;
            this.leftListener = listener;
            return this;
        }

        public Builder setLeftButton(int resId, OnClickListener listener) {
            this.leftText = context.getString(resId);
            this.leftListener = listener;
            return this;
        }

        public Builder setRightButton(String text, OnClickListener listener) {
            this.rightText = text;
            this.rightListener = listener;
            return this;
        }

        public Builder setRightButton(int resId, OnClickListener listener) {
            this.rightText = context.getString(resId);
            this.rightListener = listener;
            return this;
        }

        public CustomDialog build() {
            CustomDialog customDialog = new CustomDialog(context);
            if (!TextUtils.isEmpty(title)) {
                customDialog.setTitle(title);
            }
            if (!TextUtils.isEmpty(msg)) {
                customDialog.setMsg(msg);
            }
            if (topMsgView != null) {
                customDialog.addTopMsgView(topMsgView);
            }
            if (mainBodyView != null) {
                customDialog.addMainBodyView(mainBodyView);
            }
            if (!TextUtils.isEmpty(leftText)) {
                customDialog.setLeftButton(leftText, leftListener);
            }
            if (!TextUtils.isEmpty(rightText)) {
                customDialog.setRightButton(rightText, rightListener);
            }
            return customDialog;
        }
    }
}
